package es.titoapps.spanishhandball.entities;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;

public class New implements Comparable<New>,Parcelable{

    private String source;
	private String title;
    private String link;
    private String description;
    private String guid;
    private String date;
    private Date dateFormat;
    private long dateOrder; 
    private int enabled;
    private String content;
    private byte[] image;
    private int imageLength;
    private boolean imageLoaded;
    

	public New(){
    	imageLength=-1;
    	imageLoaded=false;
    }
    
  
    
    public void setDateOrder(int dateOrder) {
		this.dateOrder = dateOrder;
	}

	public long getDateOrder() {
		return dateOrder;
	}

	public Date getDateFormat() {
		return dateFormat;
	}

    //NOT IMPLEMENTED
//	public void setDateFormat(Date dateFormat) {
//		this.dateFormat = dateFormat;
//	}

	public String getTitle() {
        return title;
    }
 
    public String getLink() {
        return link;
    }
 
    public String getDescription() {
        return description;
    }
 
    public String getGuid() {
        return guid;
    }
 
    public String getDate() {
        return date;
    }
 
    public void setTitle(String t) {
        title = t;
    }
 
    public void setLink(String l) {
        link = l;
    }
 
    public void setDescription(String d) {
        description = d;
    }
 
    public void setGuid(String g) {
        guid = g;
    }
 
	public int getEnabled() {
		return enabled;
	}
	
	public void setEnabled(int enabled) {
		this.enabled = enabled;
	}
	
    private static int getMonth(String month){
    	
    	if(month.equalsIgnoreCase("Ene")||month.equalsIgnoreCase("Jan"))
    		return 0;
    	else if(month.equalsIgnoreCase("Feb"))
    		return 1;
    	else if(month.equalsIgnoreCase("Mar"))
    		return 2;
    	else if(month.equalsIgnoreCase("Abr")||month.equalsIgnoreCase("Apr"))
    		return 3;
    	else if(month.equalsIgnoreCase("May"))
    		return 4;
    	else if(month.equalsIgnoreCase("Jun"))
    		return 5;
    	else if(month.equalsIgnoreCase("Jul"))
    		return 6;
    	else if(month.equalsIgnoreCase("Ago")||month.equalsIgnoreCase("Aug"))
    		return 7;
    	else if(month.equalsIgnoreCase("Sep"))
    		return 8;
    	else if(month.equalsIgnoreCase("Oct"))
    		return 9;
    	else if(month.equalsIgnoreCase("Nov"))
    		return 10;
    	else if(month.equalsIgnoreCase("Dic")||month.equalsIgnoreCase("Dec"))
    		return 11;
    	else 
    		return -1;
    	
    }
    
    public static String getDetailedDateFormatWithHourUndefined(String fecha,int second){
    	
    	//obtener la fecha completa desde el formato 00/00/0000
    	//Tue, 05 Jun 2012 21:13:57 GMT
    	int in=fecha.indexOf("-");
    	int end=fecha.lastIndexOf("<");
    	fecha=fecha.substring(in+1, end);
        SimpleDateFormat inFormat = new SimpleDateFormat("dd/MM/yyyy");
        Date date = null;
		try {
			date = inFormat.parse(fecha.toString());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        SimpleDateFormat outFormat = new SimpleDateFormat("EEE, dd MMM yyyy k:mm");
        String goal = outFormat.format(date);
        if(second<10)
        	goal=goal+":0"+second+" GTM";
        else
        	goal=goal+":"+second+" GTM";
        
        return goal;
    }
    
    public static Date formatDate(String dateUnformatted){
    	
    	//Tue, 05 Jun 2012 21:13:57 GMT
        
        String day=dateUnformatted.substring(5, 7);
        String month=dateUnformatted.substring(8, 11);
        String year=dateUnformatted.substring(12, 16);
        String hour=dateUnformatted.substring(17, 19);
        String minute=dateUnformatted.substring(20, 22);
        String second=dateUnformatted.substring(23, 24);
        
        int monthNumber=New.getMonth(month);
        Calendar calendar=Calendar.getInstance();
        
        if(monthNumber!=-1)	
        	calendar.set(Integer.parseInt(year), monthNumber, Integer.parseInt(day), Integer.parseInt(hour), Integer.parseInt(minute), Integer.parseInt(second));
        else{
        	calendar.set(Integer.parseInt(year), 1, Integer.parseInt(day), Integer.parseInt(hour), Integer.parseInt(minute), Integer.parseInt(second));
        	Log.e("ERROR al parsear el mes, marcado como 1: ", month);
        }
        
        return calendar.getTime();
    	
    }
    
    public static String setTwoCharactersLengthStringForDatePart(int datePart){
    	
    	if(datePart<10)
        	return "0"+datePart;
        else
        	return Integer.toString(datePart);
    }
    
    public static String getDateOrderFormatForDate(Date date){
    	
    	Calendar calendar=Calendar.getInstance();
        calendar.setTime(date);
        
        StringBuffer s=new StringBuffer();
        s.append(calendar.get(Calendar.YEAR));
        s.append(setTwoCharactersLengthStringForDatePart(calendar.get(Calendar.MONTH)));
        s.append(setTwoCharactersLengthStringForDatePart(calendar.get(Calendar.DAY_OF_MONTH)));
        s.append(setTwoCharactersLengthStringForDatePart(calendar.get(Calendar.HOUR_OF_DAY)));
        s.append(setTwoCharactersLengthStringForDatePart(calendar.get(Calendar.MINUTE)));
        s.append(setTwoCharactersLengthStringForDatePart(calendar.get(Calendar.SECOND)));
        
        return s.toString();
    }
    
    public void setDate(String f) {
        date = f;
        this.dateFormat=New.formatDate(f);
        
        dateOrder=Long.parseLong(getDateOrderFormatForDate(dateFormat));
    }

	public String getSource() {
		return source;
	}

	public void setSource(String source) {
		this.source = source;
	}
	
	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}
	

	/**
	 * @return the image
	 */
	public byte[] getImage() {
		return image;
	}


	/**
	 * @param image the image to set
	 */
	public void setImage(byte[] image) {
		this.image = image;
		
		if(image!=null)
			this.setImageLength(image.length);
		else 
			this.setImageLength(0);
		
		this.imageLoaded=true;
	}

	
	@Override
	public int compareTo(New noticia) {
		// Compare to invertido para que salgan primero las noticias mas actuales
		return noticia.dateFormat.compareTo(this.dateFormat);
	}

	
	
	@Override
	public int describeContents() {

		return 0;
	}

	@Override
	public void writeToParcel(Parcel dest, int flags) {

		dest.writeString(source);
		dest.writeString(title);
		dest.writeString(link);
		dest.writeString(description);
		dest.writeString(guid);
		dest.writeString(date);
		dest.writeInt(enabled);
		dest.writeString(content);
		
		dest.writeInt(imageLoaded?1:0);
		
		if(image!=null)
			dest.writeInt(image.length);
		else if(content!=null)
			dest.writeInt(0);
		
		dest.writeByteArray(image);
		
	}
	
	public static final Parcelable.Creator<New> CREATOR = new Parcelable.Creator<New>() {
		public New createFromParcel(Parcel in) {
			return new New(in);
		}

		public New[] newArray(int size) {
			return new New[size];
		}
	};

	private New(Parcel in) {

		this.source=in.readString();
		this.title=in.readString();
		this.link=in.readString();
		this.description=in.readString();
		this.guid=in.readString();
		setDate(in.readString());
		this.enabled=in.readInt();
		this.content=in.readString();
		
		int imagenCargada=in.readInt();
		this.imageLoaded=(imagenCargada==1);
		
		int tamano=in.readInt();
		
		if(tamano!=0 && tamano!=-1){
			this.image = new byte[tamano]; 
			in.readByteArray(image);
		}else{
			this.image=null;
			this.imageLength=0;
		}
			
	}



	/**
	 * @return the imageLength
	 */
	public int getImageLength() {
		return imageLength;
	}



	/**
	 * @param imageLength the imageLength to set
	 */
	public void setImageLength(int imageLength) {
		this.imageLength = imageLength;
	}

	public boolean getImageLoaded() {
		return imageLoaded;
	}



	public void setImageLoaded(boolean imageLoaded) {
		this.imageLoaded = imageLoaded;
	}


}

